<style type="text/css">
    body{
        background: #ffffff;
    }
   table,tr,th,td {
        border: 1px solid #f4f4f4;
   }
   .table {
    padding: 0 6.5%;
    display: flex;
    flex-flow: row;
    align-items: center;
   }
   .table>table {
    width: 100%;
      background-color: #ffffff;
   }
   .table th,.table tr,.table td {
    text-align: center;
   }
   .table th,.table td {
    height: 50px;
   }
   .table th,.table td {
    padding: 10px 0;
   }
   .table td {
    background-color: #ffffff;
    text-align: center;
    position: relative;
   }
   .table td>input {
    margin: 0 auto;
    height: 24px;
    /*position: absolute;
    top: calc(50% - 12px);
    left: calc(50% - 7px);*/
   }
   .trs>td {
    padding: 0;
    text-align: center;
  }
   .trs>td>input {
    width: 80%;
   }
   .setMoney {
    background-color: #2c3e50;
    color: #ffffff;
    text-align: center;
    font-size: 12px;
    line-height: 30px;
    padding: 0 5px;
    border-radius: 3px;
   }
   .setMoney:hover {
    cursor: pointer;
    opacity: .7;
   }

</style>
<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
    <div class="form-group table">
        <table border="0">
            <tr>
                <th>id</th>
                <th>规格</th>
                <th>价格</th>
                <th>库存</th>
                <th>分配库存</th>
                <th>拼团满员人数</th>
                <th>满员人数价格</th>
                <th>会员等级限制</th>
                <th style="width: 90px">操作</th>
            </tr>
              {volist name='all_sku' id='vo'}
                <tr class="trs">
                  <td>{$vo.ggs_id}</td>
                  <input type="hidden" name="sgkp_id" value="{$vo.sgkp_id}">
                  <td>{$vo.goods_sku_text}</td>
                  <td style="width: 7%;">{$vo.old_price}</td>
                  <td style="width: 7%;">{$vo.stock}</td>
                  <td>
                    <input class='allow_ope' type='text' name='store' value='{$vo.store}' assigned-store="{$vo.assigned_store}">
                  </td>
                  <td><input class='allow_ope' type='text' name='person_num' value='{$vo.person_num}'></td>
                  <td><input class='allow_ope' type='text' name='price' value='{$vo.price}'></td>
                  <td>
                    <select name='valid_grade'>
                      <option value='0' {if condition="$vo.valid_grade == 0"} selected {/if}>无会员限制</option>
                      <option value='1' {if condition="$vo.valid_grade == 1"} selected {/if}>1级会员</option>
                      <option value='2' {if condition="$vo.valid_grade == 2"} selected {/if}>2级会员</option>
                    </select>
                  </td>
                  <td><p class="setMoney">设置价格区间</p></td>
                </tr> 
              {/volist}
        </table>
    </div>
    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <div  class="btn btn-success btn-embossed " id="submit">确认</div>
            
        </div>
    </div>
</form>
<input type="hidden" name="gg_id" value="{$gg_id}">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
  $(function(){
      $("#submit").click(function(){
          var arr = [];
          var gg_id = $("input[name='gg_id']").val();
          $(".trs").each(function(){
              var obj = {

                  ggs_id:$(this).children('td').eq(0).text(),
                  sgkp_id:$(this).children("input[name='sgkp_id']").val(),
                  store:$(this).children('td').find("input[name='store']").val(),
                  person_num:$(this).children('td').find("input[name='person_num']").val(),
                  price:$(this).children('td').find("input[name='price']").val(),
                  valid_grade:$(this).children('td').find("select[name='valid_grade']").val()
              }
              arr.push(obj)
          })
          var info = JSON.stringify(arr)
          $.ajax({
              type:"post",
              url:"{:url('activity/group_detail/update')}",
              data:{
                  gg_id:gg_id,
                  info:info
              },
              dataType:"json",
              success:function(msg){
                 if(msg.state == 1){
                    layer.msg('更新成功！');
                    setTimeout(function(){
                     sessionStorage.setItem('status',1)
                    },2000);
                 }else if(msg.state == 2){
                    layer.msg('更新失败！');
                 }
              }

          })
      })
      $(".allow_ope").focusout(function(){
        var name = $(this).prop('name');
        info = $(this).val();
        id = $(this).parents('tr').children("td").eq(0).text();

        switch(name)
        {
            //价格
            case 'price':
                
                price_reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                if(!price_reg.test(info)){
                  alert('金钱格式不正确');
                  return false;
                }
                old_price = $(this).parents('tr').children("td").eq(2).children("input").val();
                if(parseFloat(old_price) < parseFloat(info)){
                  alert('拼团满员价格不能大于原价');
                  return false;
                }
                break;
            //库存
            case 'store':
                
                int_reg = /(^[0-9]\d*$)/;
                if(!int_reg.test(info)){
                  alert('库存必须是正整数');
                  return false;
                }
                var assigned_store = $(this).attr("assigned-store");
                all_store = $(this).parents('tr').children("td").eq(3).text();
                allow_store = parseInt(all_store) - parseInt(assigned_store)
                if(parseInt(allow_store) < parseInt(info)){
                  alert("输入库存不能大于剩余库存,剩余库存为: "+allow_store)
                  return false;
                }
                break;
            //最大人数限制
            case 'person_num':
                int_reg = /(^[0-9]\d*$)/;
                if(!int_reg.test(info)){
                  alert('人数必须是正整数');
                  return false;
                }
                break;
            default:
            break;    
        }
      })
        //新增区域人员弹窗页面
        $(".setMoney").click(function (e) {
            // var url = document.getElementById('load_url').value;
            var ggs_id = $(this).parents('tr').children("td").eq(0).text();
            var url = '{:url("activity/group_detail/other")}?ggs_id='+ ggs_id
            var op = layer.open({
                title:'区域人员限制',
                type:2,
                content: url,
                maxmin: true,
                area: ['80%', '80%'],
                success:function(){

                    var timer = setInterval(function(){
                        var status = sessionStorage.getItem('status');
                        
                        if (status == '1') {
                            sessionStorage.clear();
                              layer.close(op);
                              window.location.reload();
                            clearInterval('timer');

                          
                        }
                    },200)
                }
            })
        
        })
  })
</script>
